package dao

import (
	"errors"
	"fmt"
	"github.com/google/uuid"
	"strconv"
	"study-plan/common/inits"
	"study-plan/common/models"
	"study-plan/common/proto/study"
	"time"
)

func GetUserInfo(name string) (user *models.User, err error) {
	err = inits.DB.Raw("select * from user where user_name =? limit 1", name).Scan(&user).Error
	if err != nil {
		return nil, err
	}
	return user, nil
}

func UserCreate(name string, password string) (user *models.User, err error) {
	user = &models.User{
		UserName: name,
		Password: password,
	}
	user_coupon := &models.UserCoupon{
		UserId:    int64(user.ID),
		CouponId:  1,
		CouponNum: 1,
	}
	tx := inits.DB
	tx.Begin()

	if err = inits.DB.Debug().Create(user).Error; err != nil {
		tx.Rollback()
		return nil, err
	}
	if err = inits.DB.Debug().Create(user_coupon).Error; err != nil {
		tx.Rollback()
		return nil, err
	}

	tx.Commit()
	return user, nil
}

func GetCatList(id int64) (carList []*study.CatList, err error) {
	var car *models.UserCar
	if err = inits.DB.Debug().Where("user_id = ?", id).Find(&car).Error; err != nil {
		return nil, err
	}
	carList = append(carList, &study.CatList{
		CourseId:       car.CourseId,
		CourseTitle:    car.CourseTitle,
		CourseDescribe: car.CourseDescribe,
		CoursePrice:    float32(car.CoursePrice),
	})
	return carList, nil
}

func GetCourse(id int64) (course *models.Course, err error) {
	if err = inits.DB.Debug().Raw("select * from course where id = ? limit 1", id).Scan(&course).Error; err != nil {
		return nil, err
	}
	return course, nil
}

func UserCarCreate(id int64, course *models.Course) (courseId int64, err error) {

	userCat := &models.UserCar{
		UserId:         id,
		CourseId:       int64(course.ID),
		CourseTitle:    course.CourseTitle,
		CourseDescribe: course.CourseDescribe,
		CourseNum:      1,
		CoursePrice:    course.CoursePrice,
	}
	price := course.CoursePrice * float64(userCat.CourseNum)
	userCat.CarPrice = price
	if err = inits.DB.Debug().Create(userCat).Error; err != nil {
		return 0, err
	}
	return int64(course.ID), nil
}

func UserCarDelCourse(id int64, cid int64) error {
	if err := inits.DB.Debug().Model(&models.UserCar{}).Delete("user_id=? and course_id=?", id, cid).Error; err != nil {
		return err
	}
	return nil
}

func OrderCreate(in *study.OrderCreateRequest, price float64, title string, carPrice float64) (order *models.Order, err error) {
	now := strconv.Itoa(int(time.Now().Unix()))

	var userCoupon models.UserCoupon

	order = &models.Order{
		UserId:      in.UserId,
		CourseId:    in.CourseId,
		OrderSn:     now + uuid.New().String(),
		OrderPrice:  price,
		OrderStatus: 1,
		Payment:     1,
		CourseTitle: title,
		CoursePrice: carPrice,
	}

	if err = inits.DB.Where("user_id=? and coupon_id=1 and coupon_num!=0", in.UserId).Find(&userCoupon).Error; err != nil {
		fmt.Println("无优惠券可用")
	} else {
		order.OrderPrice = carPrice - 5
		num := userCoupon.CouponNum - 1
		inits.DB.Model(&models.UserCoupon{}).Where("user_id=?", in.UserId).Update("coupon_num", num)
	}

	if err = inits.DB.Debug().Create(&order).Error; err != nil {
		return nil, errors.New("订单创建失败")
	}

	return order, nil

}

func GetUserCar(id int64) (car *models.UserCar, err error) {
	if err = inits.DB.Where("user_id=?", id).Find(&car).Error; err != nil {
		return nil, err
	}
	return car, nil
}
